//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************

//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>
#include <ulinux/error.h>
#include <ulinux/args_env.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>

#include <ulinux/utils/mem.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "objs.h"
#include "srv/display.h"
#include "srv/registry.h"
#include "wire/wl_display.h"
#include "wire/wl_registry.h"
#include "wire/wl_callback.h"
#include "wire/wl_shm.h"
//------------------------------------------------------------------------------

//the object id is the index in the array
struct obj{
  enum wl_itfs itf;
  void *listener;
};
struct obj objs[OBJS_MAX];

void objs_init(void)
{
  for(k_u32 id=0;id<OBJS_MAX;++id){
    objs[id].itf=WL_INVALID;
    objs[id].listener=0;
  }

  //the display singleton which always exits at id=1
  objs[WL_DISPLAY_ID].itf=WL_DISPLAY;
  objs[WL_DISPLAY_ID].listener=(void*)&display_listener;
}

k_u32 objs_add(enum wl_itfs itf,void *listener)
{
  k_u32 id;
  for(id=0;id<OBJS_MAX;++id) if(objs[id].itf==WL_INVALID) break;
  if(id<OBJS_MAX){
    objs[id].itf=itf;
    objs[id].listener=listener;
  }
  return id;
}

void objs_del(k_u32 id)
{
  if(id<REGISTRY_N_MAX) objs[id].itf=WL_INVALID;
}

void objs_ev(k_u32 id,k_u32 *ev)
{
  if(id<OBJS_MAX&&objs[id].itf!=WL_INVALID){
    void (*itf_ev)(k_u32 *ev,void *listener);
    switch(objs[id].itf){
    case WL_DISPLAY:
      itf_ev=wl_display_ev;
      break;
    case WL_REGISTRY:
      itf_ev=wl_registry_ev;
      break;
    case WL_CALLBACK:
      itf_ev=wl_callback_ev;
      break;
    case WL_SHM:
      itf_ev=wl_shm_ev;
      break;
    default:
      itf_ev=0;
    };
    if(itf_ev) itf_ev(ev,objs[id].listener);//ev is min 2 dws
  }
}
